#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/14  16:49
# @Author: 余浪人
# @email: yulangren520@gmail.com

from wtforms import Form, StringField, validators, SelectField, IntegerField, BooleanField, TextAreaField

from apps.models.column_model import Column_model


# 采集规则
class Collector_Form(Form):
    def __init__(self, *args, **kwargs):
        super(Collector_Form, self).__init__(*args, **kwargs)
        if not Column_model.query.count():
            self.class_lD.choices = [(0, '请先添加分类')]
        else:
            self.class_lD.choices = [(r.id, r.column_name) for r in Column_model.query.all()]  # 初始化栏目

    project_name = StringField(
        '项目名称', validators=[validators.DataRequired('项目名称不可为空!')],
        render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 20%;float: left;"})
    class_lD = SelectField('所属栏目', validators=[validators.DataRequired('请选择所属栏目!')], coerce=int)
    encode_code = SelectField('网站编码', choices=[(1, 'UTF-8'), (2, 'GBK'), (3, 'BIG5')], coerce=int)
    rule_cls = SelectField('规则方式', choices=[(1, 'Xpath'), (2, '正则表达式')], coerce=int)
    goal_url = StringField(
        '目标URL', validators=[validators.DataRequired('请填写目标URL!'), validators.URL(message='目标URL格式错误!')],
        render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 50%;float: left;"})
    URL = StringField(
        '列表URL', validators=[validators.DataRequired('请填写URL!'), validators.URL(message='URL格式错误!')], default='http://',
        render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 60%;"})
    URL_create = StringField(
        'URL生成', default='http://',
        render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 60%;float: left;"})
    create_range = StringField(
        '生成范围', render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 20%;float: left;"})
    create_is_range = BooleanField('批量生成', render_kw={"title": "批量生成"})
    img_save_path = StringField(
        '图片文件本地保存目录', default='image/', render_kw={"class": "layui-input", "style": "width: 40%;float: left;"})
    img_is_save = BooleanField('保存图片', render_kw={"title": "保存图片"})
    url_is_desc = BooleanField('倒序采集', render_kw={"title": "倒序采集"})
    tim_acquisition = BooleanField('定时采集', render_kw={"title": "定时采集"})
    url_relative = BooleanField('相对地址', render_kw={"title": "相对地址"})
    replace_content = TextAreaField(
        '内容替换', render_kw={"placeholder": "替换前的字符串||替换后的字符串", "class": "layui-textarea"})
    list_rule = TextAreaField('列表规则', validators=[validators.DataRequired('列表规则未添加!')],
                              render_kw={"class": "layui-textarea", "placeholder": "列表页 XPath规则 或 正则式"})
    URL_rule = TextAreaField('链接规则', validators=[validators.DataRequired('列表URL规则未添加!')],
                             render_kw={"class": "layui-textarea", "placeholder": "列表页URL XPath规则 或 正则式"})
    img_url_rule = TextAreaField('列表页小图URL规则',
                                 render_kw={"class": "layui-textarea", "placeholder": "小图片 XPath规则 或 正则式"})
    title_rule = TextAreaField('标题规则', render_kw={"class": "layui-textarea", "placeholder": "标题 XPath规则 或 正则式"})
    content_rule = TextAreaField('正文规则', validators=[validators.DataRequired('正文规则未添加!')],
                                 render_kw={"class": "layui-textarea", "placeholder": "正文 XPath规则 或 正则式"})
    keys_rule = TextAreaField('关键词规则', render_kw={"class": "layui-textarea"})
    time_rule = StringField('时间规则', render_kw={"class": "layui-input"})
    author_rule = StringField('作者规则', render_kw={"class": "layui-input"})
    source_rule = StringField('来源规则', render_kw={"class": "layui-input"})
    restrictive = IntegerField('采集限制', default=0,
                               render_kw={"class": "layui-input", "style": "width: 10%;float: left;"})


# 自动采集
class Auto_motion_Form(Form):
    project_name = StringField(
        '项目名称', validators=[validators.DataRequired('项目名称不可为空!')],
        render_kw={"class": "layui-input", "lay-verify": "required"})
    set_time = IntegerField('间隔时间', validators=[validators.DataRequired('请输入时间!')], render_kw={"class": "layui-input"})
    is_start = BooleanField('状态', render_kw={"title": "运行"})
